<?php

namespace App\Kuafu\User\Service;

use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Common\Enum\CodeEnum;
use App\Kuafu\Sys\Model\Menus;
use App\Kuafu\Sys\Service\MenusService;
use App\Kuafu\User\Model\Permissions;
use App\Kuafu\User\Model\RolePermissionMappings;

class PermissionsService extends BaseService
{

    /**
     * @var int 角色 Id
     */
    private $roleId;

    /**
     * @var int 平台类型
     */
    private $platform;

    public function __construct($brandId = 0, $shopId = 0, $roleId = 0, $platform = CodeEnum::PLATFORM_B_BRAND)
    {
        parent::__construct($brandId, $shopId);

        $this->roleId   = $roleId;
        $this->platform = $platform;
    }

    /**
     * 获取权限列表
     *
     * @param int $roleId
     * @return array
     */
    public function getPermissionsList()
    {
        // 获取所有权限
        $permissionAll = Permissions::getAll($this->platform)->toArray();

        if ($this->roleId) {
            // 获取当前用户的权限
            $rolePermissionAll = RolePermissionMappings::getAllByRoleId($this->roleId)->keyBy('permissionId')
                                                       ->toArray();

            // 判断用户当前权限
            foreach ($permissionAll as &$permissionValue) {
                $permissionValue['enable'] = key_exists($permissionValue['id'], $rolePermissionAll) ? 1 : 0;
            }
        } else {
            foreach ($permissionAll as &$permissionValue) {
                $permissionValue['enable'] = 0;
            }
        }

        switch ($this->platform) {
            case CodeEnum::PLATFORM_DEFAULT:
            case CodeEnum::PLATFORM_B_BRAND:
                // 获取所有菜单目录
                $menusAll = Menus::getAll(Menus::TYPE_WEB)->toArray();
                break;
            case CodeEnum::PLATFORM_POS:
                // 获取所有菜单目录
                $menusAll = Menus::getAll(Menus::TYPE_POS)->toArray();
                break;
            case CodeEnum::PLATFORM_BUSINESS:
                $menusAll = Menus::getAll(Menus::TYPE_SHOP)->toArray();
                break;
            default:
                $menusAll = [];
        }

        // 整理菜单
        $mixed = self::formatMenusWithPermissions($menusAll, $permissionAll);

        // 无限极菜单
        $mixed = MenusService::formatMenus($mixed);

        return $mixed;
    }

    public function getPurePermissionsList()
    {
        $permissionAll = Permissions::getAll($this->platform);

        return $permissionAll;
    }

    /**
     * 返回 菜单和权限集合
     *
     * @param $menusAll
     * @param $permissionAll
     * @return array
     */
    public static function formatMenusWithPermissions($menusAll, $permissionAll): array
    {
        $menusIdsArr = array_column($menusAll, null, 'id');

        foreach ($menusIdsArr as &$value) {
            $value['permissionList'] = [];
        }

        foreach ($permissionAll as $permissionValue) {
            if (key_exists($permissionValue['menuId'], $menusIdsArr)) {
                $menusIdsArr[$permissionValue['menuId']]['permissionList'][] = $permissionValue;
            }
        }

        return $menusIdsArr;
    }

    /**
     * 返回 菜单和权限集合 - 特殊处理
     *
     * @param $menusAll
     * @param $permissionAll
     * @return array
     */
    public static function formatMenusWithPermissionsSp($menusAll, $permissionAll): array
    {
        $menusIdsArr = array_column($menusAll, null, 'id');

        foreach ($menusIdsArr as &$value) {
            $value['permissionList'] = [];
        }

        foreach ($permissionAll as $permissionValue) {
            if (!empty($permissionValue['additionMenuId'])) {
                $ids = explode('|', $permissionValue['additionMenuId']);
                foreach ($ids as $v) {
                    if (key_exists($v, $menusIdsArr)) {
                        $menusIdsArr[$v]['permissionList'][] = $permissionValue;
                    }
                }
            }
            if (key_exists($permissionValue['menuId'], $menusIdsArr)) {
                $menusIdsArr[$permissionValue['menuId']]['permissionList'][] = $permissionValue;
            }
        }

        return $menusIdsArr;
    }

}
